﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IData_impl
{
    /// <summary>
    /// 数据接口实现
    /// </summary>
    /// 创建时间：2022-12-23 14:51:48。作者：王浩力
    public class Water_point_itemDAL_impl : BaseDAL, IWater_point_itemDAL
    {
        public IResult Add(Water_point_item info)
        {
            Snowflake snowflake = new Snowflake(1, 1);
            string sql = @$"  INSERT INTO water_point_item (
                                 Id,
                                 InputTime,
                                 TestPressure,
                                 OPTestTemp,
                                 OPWater,
                                 TestPoint,
                                 TestWater,
                                 Createtime,
                                 WaterPointId
                             )
                             VALUES (
                                 { snowflake.NextId() },
                                 $InputTime,
                                 $TestPressure,
                                 $OPTestTemp,
                                 $OPWater,
                                 $TestPoint,
                                 $TestWater,
                                 $Createtime,
                                 $WaterPointId
                             );  ";
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("$InputTime", info.InputTime);
            parameters.Add("$TestPressure", info.TestPressure);
            parameters.Add("$OPTestTemp", info.OPTestTemp);
            parameters.Add("$OPWater", info.OPWater);
            parameters.Add("$TestPoint", info.TestPoint);
            parameters.Add("$TestWater", info.TestWater);
            parameters.Add("$Createtime", info.Createtime);
            parameters.Add("$WaterPointId", info.WaterPointId);

            string error = null;
            int count = ExecSqlCmd(sql, parameters, c =>
            {
                return c.ExecuteNonQuery();
            }, ex => { error = ex.Message; });
            return count > 0 ? new ResultSuccess("新增成功") : new Result("新增失败：" + error);
        }

        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public IResult Delete(long id)
        {
            string sql = "  delete  from  water_point_item  where  id=" + id;
            string error = null;
            int count = ExecSqlCmd(sql, null, c =>
            {
                return c.ExecuteNonQuery();
            }, ex => { error = ex.Message; });
            return count > 0 ? new ResultSuccess("删除成功") : new Result("删除失败：" + error);

        }

        public List<Water_point_item> GetList(long mainKey)
        {
            string sql = " select *  from Water_point_item where  WaterPointId=" + mainKey;
            return base.ExecSqlCmd(sql, null, c =>
            {
                var reader = c.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                List<Water_point_item> list = new List<Water_point_item>();
                while (reader.Read())
                {
                    Water_point_item water_Point_Item = new Water_point_item();
                    ReaderField(reader, water_Point_Item);
                    list.Add(water_Point_Item);
                }
                return list;
            });

        }

        /// <summary>
        /// 读取数据到实体中
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="water_Point_Item"></param>
        private static void ReaderField(System.Data.Common.DbDataReader reader, Water_point_item water_Point_Item)
        {
            water_Point_Item.Id = (long)reader["id"].ToMyInt64();
            water_Point_Item.InputTime = reader["InputTime"].ToString();
            water_Point_Item.TestPressure = reader["TestPressure"].ToMyDecimal();
            water_Point_Item.OPTestTemp = reader["OPTestTemp"].ToString();
            water_Point_Item.OPWater = reader["OPWater"].ToMyDecimal();
            water_Point_Item.TestPoint = reader["TestPoint"].ToString();
            water_Point_Item.TestWater = reader["TestWater"].ToMyDecimal();
            water_Point_Item.Createtime = reader["Createtime"].ToString();
            water_Point_Item.WaterPointId = (long)reader["WaterPointId"].ToMyInt64();
        }

        /// <summary>
        /// 分页查询子项
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="p">当前页码</param>
        /// <param name="rows">当前记录数</param>
        /// <returns></returns>
        /// 创建时间：2023-1-4 11:01:36。作者：王浩力
        public PageModel<Water_point_item> GetPageItem(Water_point where, int p, int rows)
        {
            PageModel<Water_point_item> page = new PageModel<Water_point_item>();
            string sql = @$" SELECT 
m.Report_no,
k.*
  FROM water_point_item as k
  left  join water_point as m  on m.Id=k.WaterPointId  where  m.DeleteTime is null  and m.StepCurrent=4    ";
            StringBuilder whereSb = new StringBuilder();
            Dictionary<string, object> kv = new Dictionary<string, object>();
            if (where != null)
            {
                if (!string.IsNullOrWhiteSpace(where.Report_no))
                {
                    whereSb.AppendLine("  and  m.Report_no  like  $Report_no  ");
                    kv.Add("$Report_no", $"%{where.Report_no}%");
                }
                if (!string.IsNullOrWhiteSpace(where.E3) && DateTime.TryParse(where.E3, out DateTime dd1))
                {
                    whereSb.AppendLine($"  and  k.InputTime>'{dd1.AddDays(-1).ToString("yyyy-MM-dd")}' ");
                }
                if (!string.IsNullOrWhiteSpace(where.E4) && DateTime.TryParse(where.E4, out DateTime dd2))
                {
                    whereSb.AppendLine($"  and  k.InputTime<'{dd2.AddDays(1).ToString("yyyy-MM-dd")}' ");
                }
            }
            //查询总记录数
            string sqlTotal = @" select  count(1)    FROM water_point_item as k
  left join water_point as m  on m.Id = k.WaterPointId  where  m.DeleteTime is null  and m.StepCurrent=4  " + whereSb.ToString();
            page.Total = ExecSqlCmd(sqlTotal, kv, c =>
            {
                return (int)c.ExecuteScalar().ToMyInt64();
            });

            //当前页数据
            string sql2 = sql + whereSb.ToString()+$" order by  k.id asc  limit   {rows}  offset  {(p - 1) * rows}  ";
            page.List = ExecSqlCmd(sql2, kv, c =>
            {
                var reader = c.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                List<Water_point_item> list = new List<Water_point_item>();
                while (reader.Read())
                {
                    Water_point_item water_Point_Item = new Water_point_item();
                    ReaderField(reader, water_Point_Item);
                    list.Add(water_Point_Item);
                }
                return list;
            });
            return page;
        }

    }
}
